<script type="text/javascript">
$.role =
{
  toggleCap: function(role_id, cap) {
    var url = '<?php echo $this->response->makeUrl('/users/roles/toggle_cap/') ?>/'+role_id+'/'+$(cap).val()+'.json';

    $.ajax({
      url: url,
      dataType: 'json',
      success : function(data) {
        if (data.warnings.length) {
          alert(data.warnings);
        } else {
          if (data.notices.length) {
            alert(data.notices);
          }
        }
      },
      error : function(XMLHttpRequest, textStatus, errorThrown) {
        alert(XMLHttpRequest.responseText);
      }
    });
  }
}
</script>

<div class="ui-amp-container">
  <h2><?php echo __('Roles') ?>
    <?php echo $actions->buildIndexLinks() ?></h2>

  <?php if (!empty($roles)) { ?>
  <table class="ui-amp-table">
    <?php echo $this->AmpIndex->headers(array(
      array(__('Id'), 'role.id', array('class'=>'min')),
      array(__('Name'), 'role.name'),
      array(__('Level'), 'role.level'),
      __('Capabilities'),
      __('Actions')
    )) ?>

    <?php foreach ($roles as $role) { ?>
    <tr class="<?php echo $this->Cycle->next('default', 'ui-amp-even', 'ui-amp-odd') ?>">
      <td class="center"><?php echo $role->get('id') ?></td>
      <td class="center"><?php echo $role->get('name') ?></td>
      <td class="center"><?php echo $role->get('level') ?></td>
      <td>
        <?php if ($role->get('id') > 1) { ?>
          <?php if ($role->get('id') == 2) { ?>
            <p><small><?php echo __('These capabilities apply to all roles.') ?></small></p>
          <?php } ?>

          <p><small><?php echo $this->Form->checkboxes($caps, $actions->getRoleCheckedCaps($role),
            array('onchange'=>'$.role.toggleCap('.$role->get('id').', this);'),
            array('itemSeparator'=>'<br />')) ?></small></p>
        <?php } else { ?>
          <p><small><?php echo __('The Admin role has all capabilities.') ?></small></p>
        <?php } ?>
      </td>
      <td class="center"><?php echo $actions->buildIndexActions($role) ?></td>
    </tr>
    <?php } ?>
  </table>
  <?php } else { ?>
  <p><?php echo __('Zero roles found.') ?></p>
  <?php } ?>
</div>